package com.heer.gymnasium.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.R;
import com.heer.gymnasium.adapter.HomeSitesAdapter;
import com.heer.model.SitesBean;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

import static com.heer.gymnasium.constant.Constants.PAGESIZE;

/**
 * Created by heer on 17/4/13.
 */

public class SiteListActivity extends BaseRefreshActivity {
    @BindView(R.id.list)
    ListView list;

    private HomeSitesAdapter mSitesAdapter;

    public static void toSiteList(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, SiteListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        super.init();
        setContentView(R.layout.activity_site_more);
    }

    @Override
    protected void initView() {
        super.initView();
        titleBuilder.setTitleText("场馆列表");
        list.setAdapter(mSitesAdapter = new HomeSitesAdapter(mContext));
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SiteDetailsActivity.startDetails(mContext, mSitesAdapter.getItem(position).getId());
            }
        });
    }

    @Override
    protected void loadData(final boolean loadmore) {
        super.loadData(loadmore);

        mAppHelper.apiService().getHotSiteList(null, null, null, PAGESIZE, pageNo).enqueue(new BaseCallback<ApiResponse<SitesBean>>() {
            @Override
            public void onResponse(Call<ApiResponse<SitesBean>> call, Response<ApiResponse<SitesBean>> response) {
                layoutRefresh.setRefreshing(false);
                if (response.isSuccessful() && response.body().isSccessful()) {
                    if (loadmore)
                        mSitesAdapter.addItems(response.body().getData().getList());
                    else
                        mSitesAdapter.setItems(response.body().getData().getList());
                }
                if (response.body().isTokenExpire()) {
                    toLogin();
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<SitesBean>> call, Throwable t) {
                layoutRefresh.setRefreshing(false);
            }
        });
    }
}
